package com.tobe.spring_demo.mybatis;

import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;

/**
 * @author yangqifan 2015年9月14日
 * 
 */
public class MyClassPathScanner extends ClassPathBeanDefinitionScanner {
	
	private String basePackage;

	public MyClassPathScanner(BeanDefinitionRegistry registry) {
		super(registry, false);
	}

	@Override
	protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
		// TODO Auto-generated method stub
		 Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);
		 for(BeanDefinitionHolder bdh : beanDefinitions){
			 
			 GenericBeanDefinition definition = (GenericBeanDefinition) bdh.getBeanDefinition();
			 
			//  definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59
		    //  definition.setBeanClass(this.mapperFactoryBean.getClass());
			 
			 definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName());
			 definition.setBeanClass(MyMapperFactoryBean.class);
			 
			 //设置按类型加载,如果有setXXX(Type t), 会根据类型自动调用该方法, 根据type从spring容器中查找bean
			 definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
			 
		 }
		 
		 return beanDefinitions;
	}
	
		//加载接口类,spring默认不会加载接口类到bean容器中
	  @Override
	  protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
	    return (beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent());
	  }
}

